#!coding=utf-8
import threading
import time

# 创建一个锁
lock = threading.Lock()

def worker():
    # 获取锁
    lock.acquire()
    print(threading.current_thread().name, "is working...")
    try:
        # 访问共享数据
        print("i am worker thread")
        time.sleep(3)
    finally:
        # 释放锁
        print(threading.current_thread().name, "is working...")
        lock.release()

# 创建两个线程
thread1 = threading.Thread(target=worker)
thread2 = threading.Thread(target=worker)

# 启动线程
thread1.start()
thread2.start()

# 等待所有线程结束
thread1.join()
thread2.join()